package com.cx.cxapi.uninstall;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cx.cxapi.R;
import com.jws.thirdlibrary.CxManager;

import java.lang.reflect.Method;
import java.util.ArrayList;


public class WhitelistUninstallActivity extends Activity implements View.OnClickListener, WhitelistUninstallAdapter.DeleteCallback {

    private CxManager mCxManager;
    private EditText mEditText;
    private TextView mTextview;
    private Context mContext;
    private WhitelistUninstallAdapter myListViewAdapter;
    private ListView mListView;
    private ArrayList<String> mWhitelistPackages;
    private int[] intArray = {R.id.btn_add, R.id.btn_delete, R.id.btn_query, R.id.btn_exist, R.id.btn_size};
    private String TAG = WhitelistUninstallActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.uninstall_whitelist_main);
        initView();
        initData();
        initLisenter();
    }

    private void initLisenter() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                WhitelistUninstallAdapter.ListItemHoder listItemHoder = (WhitelistUninstallAdapter.ListItemHoder) view.getTag();
                CharSequence text = listItemHoder.content.getText();
                mEditText.setText(text);
                Log.i(TAG, "onItemClick: position " + position + " : view " + view.toString() + "\n" + " :text " + text);
            }
        });
    }

    private void initData() {
        mContext = getApplicationContext();
        mWhitelistPackages = new ArrayList<>();
        mCxManager = CxManager.create(mContext);
        mWhitelistPackages = mCxManager.jwsUninstallReadPackages();

        Log.i(TAG, "mWhitelistPackages: " + mWhitelistPackages.size());

        myListViewAdapter = new WhitelistUninstallAdapter(mContext);
        if (mWhitelistPackages != null) {
            myListViewAdapter.setData(mWhitelistPackages);
            myListViewAdapter.setDeleteCallback(this);
            mListView.setAdapter(myListViewAdapter);
        }
    }


    public static String getSystemProperty(String property, String defaultValue) {
        try {
            Class clazz = Class.forName("android.os.SystemProperties");
            Method getter = clazz.getDeclaredMethod("get", String.class);
            String value = (String) getter.invoke(null, property);
            if (!TextUtils.isEmpty(value)) {
                return value;
            }
        } catch (Exception e) {
            Log.d("", "Unable to read system properties");
        }
        return defaultValue;
    }

    private void initView() {
        for (int id : intArray) {
            Button btn = (Button) findViewById(id);
            btn.setOnClickListener(this);
        }
        mListView = (ListView) findViewById(R.id.listview);
        mEditText = (EditText) findViewById(R.id.edit_title);
        mTextview = (TextView) findViewById(R.id.package_query);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_add:
                String packageName = mEditText.getText().toString().trim();
                Log.i(TAG, "packageName: " + packageName);
                if (packageName == null || packageName.equals("")) {
                    return;
                }
                mCxManager.jwsUninstallInsertPackage(packageName);
                mEditText.setText("");
                ArrayList<String> appendPackages = mCxManager.jwsUninstallReadPackages();
                Log.i(TAG, "appendPackages:  appendPackages " + appendPackages.size());
                myListViewAdapter.setData(appendPackages);
                myListViewAdapter.notifyDataSetChanged();
                break;

            case R.id.btn_delete:
                String deletePackage = mEditText.getText().toString().trim();
                if (deletePackage == null || deletePackage.equals(""))
                    return;
                mCxManager.jwsUninstallDeletePackage(deletePackage);
                mEditText.setText("");
                ArrayList<String> deletePackages = mCxManager.jwsUninstallReadPackages();
                Log.i(TAG, "deletePackages:  deletePackages " + deletePackages.size());
                myListViewAdapter.setData(deletePackages);
                myListViewAdapter.notifyDataSetChanged();
                break;

            case R.id.btn_query:
                ArrayList<String> listPackages = mCxManager.jwsUninstallReadPackages();
                mTextview.setText(listPackages.toString());
                break;

            case R.id.btn_exist:
                String existPackage = mEditText.getText().toString().trim();
                boolean isPackageNameExist = false;
                isPackageNameExist = mCxManager.jwsUninstallReadPackageExists(existPackage);
                android.util.Log.i(TAG, "isPakcageNameExist: " + isPackageNameExist);
                if (isPackageNameExist) {
                    Toast.makeText(getApplicationContext(), R.string.jws_uninstall_package_whitelist_exist, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), R.string.jws_uninstall_package_whitelist_not_exist, Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.btn_size:
                int numbers = mCxManager.jwsUninstallReadPackagesSize();
                mTextview.setText(String.valueOf(numbers));
                break;
            default:
                break;
        }
    }


    @Override
    public void deletePosition(String packageName, int saveposition) {
        Log.i("MyListViewAdapter", "getView deletePosition: " + packageName + " :" + saveposition);
        mWhitelistPackages.remove(String.valueOf(saveposition));

        Log.i("MyListViewAdapter", "deletePosition:mWhitelistPackages  " + mWhitelistPackages.size());

        myListViewAdapter.setData(mWhitelistPackages);
        myListViewAdapter.notifyDataSetInvalidated();
    }

}
